package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduChapter;
import com.pilipili.zeus.eduService.entity.excel.ChapterExcelData;
import com.pilipili.zeus.eduService.service.EduChapterService;


public class ChapterListener extends AnalysisEventListener<ChapterExcelData> {
    public EduChapterService chapterService;
    public ChapterListener() {
    }
    public ChapterListener(EduChapterService chapterService) {
        this.chapterService = chapterService;
    }

    //按行读取Excel内容
    @Override
    public void invoke(ChapterExcelData chapterExcelData, AnalysisContext analysisContext) {
        if (chapterExcelData == null) {
            throw new MyException(20001, "文件数据为空");
        }
        EduChapter eduChapter = this.isExistChapter(chapterService, chapterExcelData.getCourseId(),chapterExcelData.getTitle());
        if (eduChapter == null) {
            eduChapter = new EduChapter();
            eduChapter.setCourseId(chapterExcelData.getCourseId());
            eduChapter.setTitle(chapterExcelData.getTitle());
            chapterService.save(eduChapter);

        }
    }
    private EduChapter isExistChapter(EduChapterService chapterService, String id, String title){
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",id);
        wrapper.eq("title",title);
        return chapterService.getOne(wrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
